import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:${USER}
 * Date:${YEAR}-${MONTH}-${DAY}
 * ${TIME}
 */
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
       int[] fruits ={3,3,3,1,2,1,1,2,3,3,4};
               Solution solution=new Solution();
       solution.totalFruit(fruits);
    }
}
class Solution {
    public int totalFruit(int[] f) {
        Map<Integer,Integer> map=new HashMap<Integer,Integer>();

        int ret=0;
        for(int left=0,right=0;right<f.length;right++){
            int kind=f[right];
            if(!map.containsKey(kind))map.put(kind,1);
            else map.put(kind,map.get(kind)+1);
            while(map.size()>2){
                int kind1=f[left];
                map.put(kind1,map.get(kind1)-1);
                if(map.get(kind1)==0){
                    map.remove(f[left]);
                }
                left++;
            }
            ret=Math.max(ret,right-left+1);
        }
        return ret;
    }
}